package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.pojo.vo.ContentVo;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.service.ContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("contentController")
public class ContentController {
    //开启日志
    Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Reference(check=false ,timeout=106666)
    private ContentService contentService;

    //跳广告分类页面
    @RequestMapping("contentcategoryList")
    public String contentcategoryList(){
        return "admin/content_category";
    }
    //跳广告管理页面
    @RequestMapping("contentList")
    public String contentList(Model model){
        List<TbContentCategory> contentCategories = contentService.getContentCategory();
        model.addAttribute("contentCategories",contentCategories);
        return "admin/content";
    }

    @RequestMapping("getContentInfo")
    @ResponseBody
    public Result getContentInfo(String name){
        try {
            logger.info("广告分类查询方法开始执行{}");
            List<TbContentCategory> contentCategories = contentService.getContentInfo(name);
            return Result.response(ResultEnum.SUCCESS).setData(contentCategories);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    @RequestMapping("insertContent")
    @ResponseBody
    public Result insertContent(TbContentCategory contentCategory ){
        try {
            logger.info("广告分类增加方法开始执行{}");
            contentService.insertContent(contentCategory);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    @RequestMapping("updateContentInfo")
    @ResponseBody
    public Result updateContentInfo(Long id){
        try {
            logger.info("广告分类回显方法开始执行{}");
            TbContentCategory contentCategories = contentService.updateContentInfo(id);
            return Result.response(ResultEnum.SUCCESS).setData(contentCategories);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    @RequestMapping("updateContent")
    @ResponseBody
    public Result updateContent(TbContentCategory contentCategory){
        try {
            logger.info("广告分类修改方法开始执行{}");
            contentService.updateContent(contentCategory);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    @RequestMapping("deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestParam("ids[]")Integer[] ids){
        try {
            logger.info("品牌批量删除方法开始执行{}");
            contentService.deleteBatch(ids);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    @RequestMapping("getContentInfos")
    @ResponseBody
    public Result getContentInfos(){
        try {
            logger.info("广告查询方法开始执行{}");
            List<ContentVo> contents = contentService.getContentInfos();
            return Result.response(ResultEnum.SUCCESS).setData(contents);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    @RequestMapping("insertContentss")
    @ResponseBody
    public Result insertContentss(TbContent content ){
        try {
            logger.info("广告增加方法开始执行{}");
            contentService.insertContentss(content);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    @RequestMapping("updateContentInfos")
    @ResponseBody
    public Result updateContentInfos(Long id ){
        try {
            logger.info("广告回显方法开始执行{}");
            TbContent content = contentService.updateContentInfos(id);
            return Result.response(ResultEnum.SUCCESS).setData(content);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    @RequestMapping("updateContentss")
    @ResponseBody
    public Result updateContentss(TbContent content ){
        try {
            logger.info("广告修改方法开始执行{}");
            contentService.updateContentss(content);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    @RequestMapping("deleteBatchs")
    @ResponseBody
    public Result deleteBatchs(@RequestParam("ids[]")Integer[] ids){
        try {
            logger.info("品牌批量删除方法开始执行{}");
            contentService.deleteBatchs(ids);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }


}
